local ClientOHOH = _G.ClientOHOH;

local M = ClientOHOH.class.new(script, ClientOHOH.files.OHOHOLYBUIBase());
local CallBackManager = ClientOHOH.load.CallBackManager();
local BtnHelper = ClientOHOH.load.BtnHelper();
local LanguageHelper=_G.LanguageHelper 

local CommonOHOH =_G.CommonOHOH


function InitInsts(ui)
	ui.insts = {};


	ui.insts.auto_Color ={
		on="ctrl.color.On",
		off="ctrl.color.Off"
	}

	-- 自动攻击
	ui.insts.auto_attack ={
		button="ctrl.btn.autoAttack.btn",
		name="ctrl.btn.autoAttack.des",
	} 

	--自动训练
	ui.insts.auto_train ={
		button="ctrl.btn.autoTrain.btn",
		name="ctrl.btn.autoTrain.des",
	} 

	--装备好友
	ui.insts.FriendEquip = {
		button="ctrl.btn.friendEquip.btn",
		-- name="Frame.left.Buttons.invite.TextButton.TextLabel",
		red={
			root="ctrl.btn.friendEquip.red",
			count="ctrl.btn.friendEquip.redCount"
		}
	}

	ui.insts.Gift = {
		button="ctrl.btn.gift.btn",
		time="ctrl.btn.gift.time",
		count="ctrl.btn.gift.redCount",
		countBg="ctrl.btn.gift.red",
		animator="ctrl.btn.gift.animator",
	}
	
	ui.insts.Friend = {
		button="ctrl.btn.inviteFriends.btn",
		name="ctrl.btn.inviteFriends.des",
	}

	-- 宠物
	ui.insts.pet ={
		button="ctrl.btn.pet.btn",
		-- name="Frame.left.Buttons.pets.btn.TextLabel",
		red={
			root="ctrl.btn.pet.red",
			count="ctrl.btn.pet.redCount"
		}
	} 

	ui.insts.Rebirth = {
		button="ctrl.btn.rebirth.btn",
		-- name="Frame.left.Buttons.Rebirth.TextButton.TextLabel",
	}

		
	ui.insts.setting = {
		button="ctrl.btn.setting.btn",
		-- name="Frame.right.btn2.setting.TextButton.TextLabel",
	}

	ui.insts.Shop ={
		button="ctrl.btn.shop.btn",
		-- name="Frame.left.Buttons.shop.TextButton.TextLabel",
		red={
			root="ctrl.btn.shop.red",
			count="ctrl.btn.shop.redCount"
		}
	} 

	ui.insts.property = {
		button="ctrl.btn.stats.btn",
		red={
			root="ctrl.btn.stats.red",
			count="ctrl.btn.stats.redCount"
		}
	}


	--交易
	ui.insts.Trade ={
		button="ctrl.btn.trade.btn",
		-- name="Frame.left.Buttons.Trade.TextButton.TextLabel",
	} 

	--Vip购买
	ui.insts.VipBuy ={
		button="ctrl.btn.vip.btn",
		root="ctrl.btn.vip.root",
		-- name="Frame.left.Buttons.VIP.TextButton.TextLabel",
	} 

	ui.insts.weapon ={
		button="ctrl.btn.weapon.btn",
		red={
			root="ctrl.btn.weapon.red",
			count="ctrl.btn.weapon.redCount",
		}
	} 



------------------------------------------------------------

	ui.insts.Ugc ={
		button="ctrl.activity.ugc.btn",
		-- name="Frame.right.btn1.ugc.btn.TextLabel",
	} 

	ui.insts.StarterPack ={
		root="ctrl.activity.StarterPack.root",
		button="ctrl.activity.StarterPack.btn",
		-- name="Frame.left.StarterPack.btn.store.text",
		tipTime="ctrl.activity.StarterPack.tipTime",
	} 


	ui.insts.base_power ={
		button="ctrl.currency.Strength.btn",
		num="ctrl.currency.Strength.num",
		image="ctrl.currency.Strength.image",
	} 
	
	ui.insts.base_wins ={
		button="ctrl.currency.wins.btn",
		num="ctrl.currency.wins.num",
		image="ctrl.currency.wins.image",
	} 

	ui.insts.base_diamond ={
		button="ctrl.currency.Diamond.btn",
		num="ctrl.currency.Diamond.num",
		image="ctrl.currency.Diamond.image",
	} 

	ui.insts.base_meat ={
		button="ctrl.currency.food.btn",
		num="ctrl.currency.food.num",
		image="ctrl.currency.food.image",
	} 

	
	ui.insts.HomeShow ={
		button="ctrl.HomeShow.btn",
		root="ctrl.HomeShow.root",
	} 

	ui.insts.Spin = {
		button="ctrl.Spin.btn",
		time="ctrl.Spin.time",
		count="ctrl.Spin.redcount",
		countBg="ctrl.Spin.red",
		center="ctrl.Spin.center",
	}


	ui.insts.Home = {
		button="ctrl.btn.home.btn",
		-- name="Frame.right.home.Day.TextButton.TextLabel",
	}

	--OP蛋
	ui.insts.OpEgg={
		button="ctrl.activity.OpEgg.btn",
		Icon="ctrl.activity.OpEgg.icon",

		red={
			root="ctrl.activity.OpEgg.red",
			count="ctrl.activity.OpEgg.redCount",
		},		-- FreeTips="Frame.right.btn1.OPEgg.btn.Text",
		CountDownTime="ctrl.activity.OpEgg.countDownTime",

	}

	--60分钟在线奖励
	ui.insts.Quest={
		button="ctrl.activity.Quest.btn",
		-- tips="Frame.left.timeGift.btn.Frame.Text",
		-- text="Frame.left.timeGift.btn.Frame.TextLabel",
		red="ctrl.activity.Quest.red",
		num="ctrl.activity.Quest.redNum",
	}
end


function M.new(...)
	return M(...);
end;

function M.SetGamePassPackStateAndClickHandle(self,stateData,clickHanlde)
	for k,v in pairs(stateData) do
		self.insts.GamePassPack[k].Parent.Visible=(not v)
		if clickHanlde then
			CallBackManager:AddRbxConnect(self, self.insts.GamePassPack[k].Activated, function()
				clickHanlde(k)
			 end);
		end
	end
end

function M.ctor(self)
	InitInsts(self)
	
	self.CallFucEvent=_G.CommonOHOH.load.Connect().new() --HomeGui函数被调研事件

	M.super.ctor(self, ClientOHOH:GetPath_Loop("ReplicatedStorage.Resource.UI.HomeGui"));

	self.TopItemList={}

	for _,v in pairs(self:WaitForChild_Loop("ctrl.TopHide"):GetChildren()) do
		table.insert(self.TopItemList,v.Value)
	end


	self:SetPetRed()
	self:SetPositionRed()
	self:SetFriendEquipRed()



	CallBackManager:AddRbxConnect(self,self.insts.HomeShow.button.Activated, function()
		self.insts.HomeShow.root.Visible=not self.insts.HomeShow.root.Visible
	end);

	self.insts.auto_attack.name.Text=LanguageHelper:GetText(30011) 
	self.insts.auto_train.name.Text=LanguageHelper:GetText(30010) 

	self:PlaySpin()	
end;


function M.dtor(self)
	M.super.dtor(self);
end;

function M.GetItemAbsoluteProperties(self,item_id)	

	local ui= self.insts[item_id].image
	return ui.AbsolutePosition,ui.AbsoluteSize

end

function M.AddItem(self,item_id,count)	
	local lable= self.insts[item_id].num
	require(lable.AddCtrl.Animation).play(count)
	-- self.CallFucEvent:Action("AddItem",item_id,count)
end

function M.HideTop(self)
	for _,ui in pairs(self.TopItemList) do
		ui.Visible=false
	end
	self.CallFucEvent:Action("HideTop")

end

function M.ShowTop(self)
	for _,ui in pairs(self.TopItemList) do
		ui.Visible=true
	end
	self.CallFucEvent:Action("ShowTop")
end

--设置拥有点数
function M.SetHadPoint(self,count)
	count=count or 0
	if  count>0 then
		self.insts.property.red.root.Visible=true
	else
		self.insts.property.red.root.Visible=false
	end

	self.insts.property.red.count.Text=count

	self.CallFucEvent:Action("SetHadPoint",count)
end

--新增武器数量
function M.SetWeaponRed(self,count)
	count=count or 0
	if  count>0 then
		self.insts.weapon.red.root.Visible=true
	else
		self.insts.weapon.red.root.Visible=false
	end

	self.insts.weapon.red.count.Text=count

	self.CallFucEvent:Action("SetWeaponRed",count)
end

--新增武器数量
function M.SetOPEggRed(self,count)
	count=count or 0
	if  count>0 then
		self.insts.OpEgg.red.root.Visible=true
	else
		self.insts.OpEgg.red.root.Visible=false
	end
	self.insts.OpEgg.red.count.Text=count

	self.CallFucEvent:Action("SetOPEggRed",count)
end


function M.ShowCanSigIn(self)

	-- self.insts.DaySin.red.root.Visible=true
	-- self.ShowCanSigInConnect=self:AddProcess(function()
	-- 	require(self.insts.DaySin.Animator).NewTween:Play()
	-- 	while self.insts.DaySin.red.root.Visible do
	-- 		self:Wait(10)
	-- 		require(self.insts.DaySin.Animator).NewTween:Play()
	-- 	end
	-- end)

end

function M.HideCanSigIn(self)
	self.insts.DaySin.red.root.Visible=false
	if self.ShowCanSigInConnect then
		self.ShowCanSigInConnect:Disconnect()
		self.ShowCanSigInConnect=nil
	end
end


function M.UpdateGiftInfo(self,count,timeStr)
	if count>0 then
		self.insts.Gift.countBg.Visible=true
		self.insts.Gift.count.Text=tostring(count)
		self.insts.Gift.time.Text=LanguageHelper:GetText(10406)
		
		if not self.PlayAnimatorTime then
			require(self.insts.Gift.animator).NewTween:Play()
			self.PlayAnimatorTime=os.time()
		elseif os.time()-self.PlayAnimatorTime>10  then
			require(self.insts.Gift.animator).NewTween:Play()
			self.PlayAnimatorTime=os.time()
		end
		
	else

		self.insts.Gift.time.Text=LanguageHelper:GetText(10405,{"%%ARG1"},{timeStr})
		self.insts.Gift.countBg.Visible=false
	end
	
	self.CallFucEvent:Action("UpdateGiftInfo",count,timeStr)
	
end



local TweenHelper = CommonOHOH.load.TweenHelper();
local tw1 = TweenInfo.new(3, Enum.EasingStyle.Linear,Enum.EasingDirection.Out,-1);
function M.PlaySpin(self)
    self.insts.Spin.center.Rotation=0
	TweenHelper:PlayTween(self.insts.Spin.center, tw1, {
		Rotation =360
	}); 
end

function M.UpdateSpinInfo(self,count,timeStr)
	if count>0 then
		self.insts.Spin.countBg.Visible=true
		self.insts.Spin.count.Text=tostring(count)
		self.insts.Spin.time.Text=LanguageHelper:GetText(10403)
	else
		self.insts.Spin.time.Text=timeStr
		self.insts.Spin.countBg.Visible=false
	end

	self.CallFucEvent:Action("UpdateSpinInfo",count,timeStr)

end

function M.SetPositionRed(self,count)	
	if not count then
		self.insts.Shop.red.count.Text="0"
		self.insts.Shop.red.root.Visible=false
	else
		self.insts.Shop.red.root.Visible=true
		local oldCount=tonumber(self.insts.Shop.red.count.Text)
		self.insts.Shop.red.count.Text=tostring(count+oldCount) 
	end
	self.CallFucEvent:Action("SetPositionRed",count)
end

function M.SetPetRed(self,count)	
	if not count then
		self.insts.pet.red.count.Text="0"
		self.insts.pet.red.root.Visible=false
	else
		self.insts.pet.red.root.Visible=true
		local oldCount=tonumber(self.insts.pet.red.count.Text)
		self.insts.pet.red.count.Text=tostring(count+oldCount) 

	end
	self.CallFucEvent:Action("SetPetRed",count)
end

function M.SetFriendEquipRed(self,count)	
	
	if not count then
		self.insts.FriendEquip.red.count.Text="0"
		self.insts.FriendEquip.red.root.Visible=false
	else
		self.insts.FriendEquip.red.root.Visible=true
		local oldCount=tonumber(self.insts.FriendEquip.red.count.Text)
		self.insts.FriendEquip.red.count.Text=tostring(count+oldCount) 

	end
	self.CallFucEvent:Action("SetFriendEquipRed",count)
end


--添加好友加成
function M.AddFriendBoost(self,boost)
	self.insts.Friend.name.Text=math.floor(boost*100+0.5).."%"
	self.CallFucEvent:Action("AddFriendBoost",boost)
end

--隐藏R9Pack
function M.HideR9Pack(self)
	self.insts.r9pack.root.Visible=false
	self.CallFucEvent:Action("HideR9Pack")
end

--隐藏VIP
function M.HideVip(self)
	self.insts.VipBuy.root.Visible=false
	self.CallFucEvent:Action("HideVip")
end

local BigNumberHelper = ClientOHOH.load.BigNumberHelper();

--填充基础信息
function M.UpdBaseData(self,w,d,m,p)
	-- self.insts.base_power.num.Text=BigNumberHelper:ToString(p)
	self.insts.base_wins.num.Text=BigNumberHelper:ToString(w)
	self.insts.base_diamond.num.Text=BigNumberHelper:ToString(d)
	self.insts.base_meat.num.Text=BigNumberHelper:ToString(m)
	self.CallFucEvent:Action("UpdBaseData",w,d,m,p)
end

--自动训练
function M.SetAutoTrain(self,isAuto)
	-- print("AUTO--Train>",isAuto)
	if isAuto then
		self.insts.auto_train.name.Text=LanguageHelper:GetText(141)..LanguageHelper:GetText(126)
		self.insts.auto_train.button.ImageColor3=self.insts.auto_Color.on
	else
		self.insts.auto_train.name.Text=LanguageHelper:GetText(141)..LanguageHelper:GetText(127)
		self.insts.auto_train.button.ImageColor3=self.insts.auto_Color.off
	end
	self.CallFucEvent:Action("SetAutoTrain",isAuto)
end;

--自动攻击
function M.SetAutoAttack(self,isAuto)
	-- print("AUTO--Attack>",isAuto)
	if isAuto then
		self.insts.auto_attack.name.Text=LanguageHelper:GetText(140)..LanguageHelper:GetText(126)
		self.insts.auto_attack.button.ImageColor3=self.insts.auto_Color.on
	else
		self.insts.auto_attack.name.Text=LanguageHelper:GetText(140)..LanguageHelper:GetText(127)
		self.insts.auto_attack.button.ImageColor3=self.insts.auto_Color.off
	end
	self.CallFucEvent:Action("SetAutoAttack",isAuto)
end;

function M.GetHeadUISize(self)
	local size=self.insts.PlayerHead.AbsoluteSize
	return UDim2.fromOffset(size.X,size.X)
end

function M.GetHeadUIPos(self,scale)
	local size=self.insts.Line.AbsoluteSize
	local pos=self.insts.PlayerHead.AbsolutePosition	
	return UDim2.fromOffset(pos.X+size.X*scale,pos.Y)
end


local GeneralTool=_G.CommonOHOH.load.GeneralTool();
local PetTool=_G.CommonOHOH.load.PetTool();

function M.ShowLimitBuyTag(self,data)
	if self.LimitBuyConnect then
		return
	end
	self.insts.LimitBuy.Root.Visible=true

	self.LimitBuyData=data
	self.insts.LimitBuy.Title.Text=data.Title
	self.insts.LimitBuy.Time.Text=GeneralTool.secondsToHHMM(self.LimitBuyData.Time)
	self.TweenLimitBuy=TweenHelper:PlayTween(self.insts.LimitBuy.Rarity, TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut,-1,true), {
		Rotation =360 --关闭状态位置
	});
	self.LimitBuyPet= PetTool.GetPetModelByPetId(data.petId)
	self.LimitBuyPet.Parent=self.insts.LimitBuy.ViewPort
    self.LimitBuyPet:SetPrimaryPartCFrame(self.insts.LimitBuy.CF.Value)
    PetTool.RoationPet(self.LimitBuyPet)
	self.LimitBuyConnect=self:AddProcess(function()
		while true do
			if self.LimitBuyData.Time<=0 then
				self:CloseLimitBuyTag()
				return
			end
			self.insts.LimitBuy.Time.Text=GeneralTool.secondsToHHMM(self.LimitBuyData.Time)
			self:Wait(1)
			self.LimitBuyData.Time=self.LimitBuyData.Time-1
		end
	end)
end

function M.CloseLimitBuyTag(self)
	self.insts.LimitBuy.Root.Visible=false

	if self.TweenLimitBuy then
		self.TweenLimitBuy:Cancel()
		self.TweenLimitBuy=nil
	end
	if self.LimitBuyPet then
		self.LimitBuyPet:Destroy()
		self.LimitBuyPet=nil
	end

	if self.LimitBuyConnect then
		self.LimitBuyConnect:Disconnect()
		self.LimitBuyConnect=nil
	end

end

return M
